package com.wing.cms.ext.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.wing.cms.ext.domain.CmsFunction;
import com.wing.common.CommonSpringJDBC;
import com.wing.common.Constants;
import com.wing.common.util.PageBean;
import com.wing.sys.domain.Auth;

@Repository
public class CmsFunctionDao extends CommonSpringJDBC<CmsFunction, String> {

	public PageBean findPageList(int currentPage, int pageSize, String siteId) {
		String where = " 1=1 ";
		String sql1 = " SELECT * FROM cms_function where " + where
				+ " ORDER BY id ASC " + " LIMIT " + (currentPage - 1)
				* pageSize + "," + pageSize;
		String sql2 = "select count(id) from  cms_function  where" + where;
		if (!Constants.WORK_SITE.equals(siteId)) {
			where = " t.ispub='" + CmsFunction.ISPUB_TRUE + "' or (a.type='"
					+ Auth.TYPE_FUNCTION + "' and a.owner_id='" + siteId
					+ "' and a.res_id=t.id) ";
			sql1 = " SELECT distinct t.* FROM cms_function t,sys_auth a where "
					+ where + " ORDER BY t.id ASC " + " LIMIT "
					+ (currentPage - 1) * pageSize + "," + pageSize;
			sql2 = "select count(t.id) FROM cms_function t,sys_auth a  where"
					+ where;
		}
		String[] sqlArray = { sql1, sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}
	
	public List getFunctionsBySiteId(String siteId) {
		String where = "";
		String sql = "";
		where = " t.ispub='" + CmsFunction.ISPUB_TRUE + "' or (a.type='"
				+ Auth.TYPE_FUNCTION + "' and a.owner_id=? and a.res_id=t.id) ";
		sql = " SELECT distinct t.* FROM cms_function t,sys_auth a where "
				+ where + " ORDER BY t.id ASC " ;
		return this.getSimpleJdbcTemplate().queryForList(sql, siteId);
	}
	
	public CmsFunction findById(String id) {
		String where = "";
		String sql = " SELECT t.* FROM cms_function t where t.id=? " ;
		List l = this.getSimpleJdbcTemplate().queryForList(sql, id);
		if(l.size()==1){
			return super.findById(id);
		}
		return null;
	}

	public Map findByFun_code(String fun_code) {
		String where = "";
		String sql = " SELECT t.* FROM cms_function t where t.fun_code=? " ;
		List l = this.getSimpleJdbcTemplate().queryForList(sql, fun_code);
		if(l.size()==1){
			return (Map)l.get(0);
		}
		return null;
	}
	
	
}
